#include "readpair.h"
#include <set>
#include "log.h"

using namespace std;

spread str2spread(string s){
  spread sp;
  if (!s.empty()){
    size_t len=s.size();
    int j=0;
    size_t k=0;
    for(size_t i=0;i<len;++i){
      if (s[i]==','){
        s[i]='\0';
        j++;
        switch(j){
          case 1:
            sp.master = &s[k];
            //TTPrint("%s\n",sp.master.c_str());
            break;
          case 2:
            sp.slave = &s[k];
            //TTPrint("%s\n",sp.slave.c_str());
            break;
          case 3:
            sp.s_threshold = ::atof(&s[k]) + BIDASKSPREAD;
            //TTPrint("sell if over %f\n",sp.short_spread_threshold);
            break;
          case 4:
            sp.liq_upper = ::atof(&s[k]);
            //TTPrint("%f\n",sp.liq_upper);
            break;
          case 5:
            sp.liq_lower = ::atof(&s[k]);
            //TTPrint("%f\n",sp.liq_lower);
            break;
          case 6:
            sp.l_threshold = ::atof(&s[k]) - BIDASKSPREAD;
            //TTPrint("sell if over %f\n",sp.short_spread_threshold);
            break;
        }
        k=i+1;
      }
    }
    sp.unit = ::atoi(&s[k]);
    //TTPrint("buy if under %f\n",sp.long_spread_threshold);
    //TTPrint("%d\n",j);
  }
  return sp;
}

vector<spread> getspreadinfo(const string& pairfile){
  ifstream f(pairfile.c_str());
  vector<spread> vs;
  set<string> master_slave;
  string x;
  TTPrint("**************************************************************\n");
  while(f.is_open() && f.good()){
    getline(f,x);
    if(!x.empty() && x[0]!='#'){
      spread tmp = str2spread(x);
      string ms=tmp.master+tmp.slave;
      if (master_slave.find(ms)==master_slave.end()){
          TTPrint("pair info:%s\n",x.c_str());
          master_slave.insert(ms);
          vs.push_back(tmp);
      }
    }
  }
  TTPrint("**************************************************************\n");
  return vs;
}

//int main(int argc, char* argv[]){
//  getspreadinfo("pairs.csv");
//  return 0;
//}


